//
// 引入 SDK
const crawlab = require('crawlab-sdk')
// const crawlab = {
//   saveItem: console.log,
//   close: () => console.log('close')
// }
const axios = require('axios')
const cheerio = require('cheerio')
const schoolName = '华中科技大学'
const baseURL = 'http://news.hust.edu.cn/'
// 这是一个结果，需要为 object 类型
// const result = {name: 'crawlab'};

;(async () => {
  crawPage(`${baseURL}/zhxw.htm`, rule)
})()

async function crawPage(url, func) {
  const { data: html } = await axios(url)
  const $ = cheerio.load(html)
  func && func($)
}

async function rule($) {
  const res = $('.list_item')
  for (let i = 0; i < res.length; i++) {
    const item = $(res[i])
    const href = baseURL + item.find('a').attr('href')
    const title = item.find('a').text().trim()
    const date = item.find('.Article_PublishDate').text()
    await crawlab.saveItem({
      time: date,
      url: href,
      title,
      schoolName,
    })
  }
  let next = $('a.Next')
    .filter(function (i, el) {
      return $(this).text() === '下页'
    })
    .attr('href')
  if (!next.match(/zhxw/)) {
    next = '/zhxw/' + next
  }
  // const next = $('.Next').attr('href')
  if (next && next !== 'javascript:void(0);') {
    crawPage(`${baseURL}${next}`, rule)
  } else {
    await crawlab.close()
  }
}
